package com.zkr.legalperson.conterllor;


import com.zkr.legalperson.model.LegalPersonAddVo;
import com.zkr.legalperson.model.LegalPersonSelectVo;
import com.zkr.legalperson.model.LegalPersonUpdateStatusVo;
import com.zkr.legalperson.model.LegalPersonUpdateVo;
import com.zkr.legalperson.service.LegalPersonService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lenovo on 2020/12/16.
 */
@RestController
@CrossOrigin
public class LagalPersonConterllor {
    @Autowired
    private LegalPersonService legalPersonService;

    //分页查询
    @PostMapping("legalPlant/query")
    public ResultVo queryPage(@RequestBody LegalPersonSelectVo legalPersonSelectVo){

        return legalPersonService.query(legalPersonSelectVo);
    }

    /**
     * 法人下拉框
     */

    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo queryLegalPerson(){
        //获取id
         Integer userid = 1;
        //根据id获取法人信息
        return legalPersonService.queryLegalPersonById(userid);
    }


    //添加
    @PostMapping("legalPlant/add")
    public ResultVo add(@RequestBody @Valid LegalPersonAddVo legalPersonAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.add(legalPersonAddVo);
    }



   //修改
   @PostMapping("legalPlant/update")
    public ResultVo update(@RequestBody @Valid LegalPersonUpdateVo legalPersonUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.update(legalPersonUpdateVo);
    }

    //修改启用禁用状态
    @PostMapping("legalPlant/updatestatus")
    public ResultVo updateStatus(@RequestBody @Valid LegalPersonUpdateStatusVo legalPersonUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.updateStatus(legalPersonUpdateStatusVo);
    }





}
